﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.Drawing;
namespace syc.Sale.Report
{
    public partial class 管材到货记录 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.Date1.Text = DateTime.Today.ToString("yyyy年MM月dd日");
                this.Date2.Text = DateTime.Today.ToString("yyyy年MM月dd日");
               
            }
            string sql = @"        SELECT  
    dbo.原料供货单位.名称,
    dbo.原料.品名, 
    dbo.原料.规格,
    dbo.原料到发记录.到货日期,
    sum(原料到发记录.总重量) as 名义重量,
    sum(原料到发记录.磅重) as 磅重,
    (sum(原料到发记录.总重量)- sum(原料到发记录.磅重)) as 磅差,
    SUM(原料到发记录.总支数) as 支数
FROM    dbo.原料到发记录  inner join dbo.原料
on 原料到发记录.原料ID=原料.ID inner join 原料供货单位
on 原料到发记录.供货ID=原料供货单位.ID
 where 原料到发记录.到货日期 between '*Date1' and '*Date2'
   GROUP BY dbo.原料.品名,dbo.原料供货单位.名称,dbo.原料.规格,原料到发记录.到货日期";
            sql = sql.Replace("*Date1", DateTime.Parse(this.Date1.Text).ToString());
            sql = sql.Replace("*Date2", DateTime.Parse(this.Date2.Text).ToString());
            this.SqlDataSource1.SelectCommand = sql;
            GridViewHelper helper = new GridViewHelper(this.GridView1);
            helper.RegisterGroup("品名", true, true);
            helper.RegisterSummary("名义重量", SummaryOperation.Sum, "品名");
            helper.RegisterSummary("磅重", SummaryOperation.Sum, "品名");
            helper.RegisterSummary("磅差", SummaryOperation.Sum, "品名");
            helper.RegisterSummary("支数", SummaryOperation.Sum, "品名");
            helper.GroupHeader += new GroupEvent(helper_GroupHeader);
            helper.GroupSummary += new GroupEvent(helper_GroupSummary);
            helper.ApplyGroupSort();
            this.GridView1.DataBind();
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

             
        }
        //自定义分组标题样式
        private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            row.Cells[0].Height = 33;
            row.BackColor = Color.LightGray;
            row.Cells[0].Text = "&nbsp;&nbsp;" + row.Cells[0].Text;

        }
        //自定义合计标题样式
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
            row.Cells[0].Text = "合计:";
            row.Height = 20;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "utf-8";
            // If you want the option to open the Excel file without saving than
            // comment out the line below
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            //设置EXCEL的表头信息
            var title = "管材到货记录";
            HttpContext.Current.Response.Write(
    @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html>
<head>
<meta http-equiv=""Content-Language"" content=""zh-cn""/>
<meta http-equiv=""Content-Type"" content="""
    + HttpContext.Current.Response.ContentType + "; charset=utf-8\"/></head><body><h2><center>" + title + "</center></h2>");
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            Response.End();

        }

        //为了导出而重载的一个事件，具体看EXPORT TO EXCEL的文章
        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the
            //specified ASP.NET server control at run time.
        }

    }
}
